//
//  AgentInfoViewController.m
//  BaseProject
//
//  Created by 王洋 on 2021/3/3.
//  Copyright © 2021 王洋. All rights reserved.
//

#import "AgentReviewModel.h"
#import "AgentInfoViewController.h"
#import "CommonTFEditViewController.h"

@interface AgentInfoViewController ()

@property (weak, nonatomic) IBOutlet UILabel *cityLab;
@property (weak, nonatomic) IBOutlet UILabel *nameLab;
@property (weak, nonatomic) IBOutlet UILabel *phoneLab;
@property (weak, nonatomic) IBOutlet UILabel *recommanderLab;

@property (weak, nonatomic) IBOutlet UIView *nameEditControl;

@property (weak, nonatomic) IBOutlet UIView *phoneEditControl;

@property (nonatomic, strong) AgentReviewModel *model;

@end

@implementation AgentInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self request_agentInfo];

}

- (void)initView {
    
    self.title = @"我的资料";
    self.view.backgroundColor = UIColor.whiteColor;
    

    WEAKSELF
    [self.nameEditControl addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
            
        CommonTFEditViewController *vc = [[CommonTFEditViewController alloc] init];
        vc.title = @"修改姓名";
        vc.editTF.placeholder = @"请填写姓名";
        vc.placeholderStr = @"请填写姓名";
        vc.contentStr = weakSelf.model.name;
        vc.completion = ^(NSString * _Nonnull resultStr) {
            weakSelf.model.name = resultStr;
            [weakSelf request_updateAgentInfo];
        };
        [weakSelf.navigationController pushViewController:vc animated:YES];
        
    }];
    
    [self.phoneEditControl addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
            
        CommonTFEditViewController *vc = [[CommonTFEditViewController alloc] init];
        vc.title = @"修改手机号";
        vc.editTF.placeholder = @"请填写手机号";
        vc.placeholderStr = @"请填写手机号";
        vc.contentStr = weakSelf.model.phone;
        vc.completion = ^(NSString * _Nonnull resultStr) {
            weakSelf.model.phone = resultStr;
            [weakSelf request_updateAgentInfo];
            
        };
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
}

- (void)fillViewWithModel:(AgentReviewModel *)model {
    self.cityLab.text = model.cityName;
    self.nameLab.text = model.name;
    self.phoneLab.text = model.phone;
    self.recommanderLab.text = model.recommendName;
}

- (void)request_updateAgentInfo {
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_ApplyRecommender];
    NSMutableDictionary *dic = @{}.mutableCopy;
    dic = [self.model modelToJSONObject];
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在更新数据"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                [weakSelf request_agentInfo];
            } else {
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}

- (void)request_agentInfo {
        
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_AgentInfo];
    
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在获取我的资料"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if ([resultDic isKindOfClass:[NSDictionary class]]) {
                    
                    NSDictionary *userApplyRecommender = [resultDic valueForKey:@"userApplyRecommender"];
                    if ([userApplyRecommender isKindOfClass:[NSDictionary class]]) {
                        weakSelf.model = [AgentReviewModel modelWithDictionary:userApplyRecommender];
                        [weakSelf fillViewWithModel:weakSelf.model];
                    }
                }
            }
        }
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}

@end
